class Party extends PIXI.Container {
	constructor(uimanager) {
		super();

		this.test();
		this.uimanager = uimanager;
		this.maxColumn = 5;

		this._selectedIndex = -1;

		// 添加背景
		const bg = new Background(uimanager);
		this.addChild(bg);

		// 添加选择栏
		this.selector = new PIXI.smooth.SmoothGraphics();
		this.selector.beginFill(0xE4F27C, 0.8);
		this.selector.lineStyle({ color: 0x58D088, width: 1, alignment: 0 });
		this.selector.drawEllipse(0, 0, 64, 29);
		this.selector.endFill();
		this.selector.position=this.getLocation(0);
		this.addChild(this.selector);

		// 添加队伍角色
		for(var i = 0; i < global.party.length; i++) {
			const character = global.party[i];
			// 添加角色
			character.centerPoint = this.getLocation(i);
			this.addChild(character);

			// 添加角色名称
			const nameLiteral = character.name || "unknown";
			const nameText = new PIXI.Text(nameLiteral, nameStyle);
			nameText.x = character.centerPoint.x + -PIXI.TextMetrics.measureText(nameLiteral, nameStyle).width / 2;
			nameText.y = character.centerPoint.y;
			nameText.resolution = 2;
			this.addChild(nameText);
		}

		uimanager.addChild(this);
		this.select(0);
	}

	getLocation(index) {
		const intRow = parseInt(index / this.maxColumn);
		const x = index % this.maxColumn * 96 + (intRow % 2 == 0 ? 130 : 80);
		const y = intRow * 70 + 42;
		return {x,y};
	}

	select(index) {
		if(this._selectedIndex == index)
			return;

		this._selectedIndex = index;

		// 如果存在之前的信息栏则移除
		if(this.info)
			this.removeChild(this.info);

		// 得到选择的character
		const character = global.party[index];

		// 添加信息栏
		this.info = new PIXI.Container();
		this.info.position={x:0,y:this.uimanager.realScreen.height-120}
		this.addChild(this.info);

		// 添加信息栏背景
		var infoBg = new PIXI.smooth.SmoothGraphics();
		infoBg.beginFill(0x0000FF, 0.5);
		infoBg.drawRoundedRect(-100, 30, 430, 200, 100);
		infoBg.endFill();
		this.info.addChild(infoBg);

		// 添加信息栏半身像
		if(character.avater) {
			var texture = PIXI.Texture.from(character.avater);
			const avater = new PIXI.Sprite(texture);
			this.info.addChild(avater);
		}

		// 添加名称
		const nameLiteral = character.name || "unknown";
		const nameText = new PIXI.Text(nameLiteral, textStyle);
		nameText.x = 165;
		nameText.y = 40;
		nameText.resolution = 2;
		this.info.addChild(nameText);

		// HP/MP
		const hpHeadLiteral = "HP ";
		const mpHeadLiteral = "SP ";
		const hpHeadText = new PIXI.Text(hpHeadLiteral, hpStyle);
		const mpHeadText = new PIXI.Text(mpHeadLiteral, spStyle);
		hpHeadText.x = 165;
		mpHeadText.x = 165;
		hpHeadText.y = 67;
		mpHeadText.y = 87;
		hpHeadText.resolution = 2;
		mpHeadText.resolution = 2;
		this.info.addChild(hpHeadText);
		this.info.addChild(mpHeadText);

		const hpLiteral = character.hp;
		const mpLiteral = character.sp;
		const hpText = new PIXI.Text(hpLiteral, textStyle);
		const mpText = new PIXI.Text(mpLiteral, textStyle);
		hpText.x = 200;
		mpText.x = 200;
		hpText.y = 65;
		mpText.y = 85;
		hpText.resolution = 2;
		mpText.resolution = 2;
		this.info.addChild(hpText);
		this.info.addChild(mpText);

		// lv/exp
		const lvHeadLiteral = "lv.";
		const expHeadLiteral = "exp.";
		const lvHeadText = new PIXI.Text(lvHeadLiteral, smallStyle);
		const expHeadText = new PIXI.Text(expHeadLiteral, smallStyle);
		lvHeadText.x = 10+5;
		expHeadText.x = 50+5;
		lvHeadText.y = 102;
		expHeadText.y = 102;
		lvHeadText.resolution = 2;
		expHeadText.resolution = 2;
		this.info.addChild(lvHeadText);
		this.info.addChild(expHeadText);

		const lvLiteral = character.level;
		const expLiteral = character.exp;
		const lvText = new PIXI.Text(lvLiteral, textStyle);
		const expText = new PIXI.Text(expLiteral, textStyle);
		lvText.x = 24+5;
		expText.x = 74+5;
		lvText.y = 100;
		expText.y = 100;
		lvText.resolution = 2;
		expText.resolution = 2;
		this.info.addChild(lvText);
		this.info.addChild(expText);

		// 播放选择器动画
		this.selectorTween = createjs.Tween.get(this.selector.position).to(this.getLocation(index),200);
		this.info.position.x = -400;
		this.infoTween = createjs.Tween.get(this.info.position).to({x:0,y:this.info.position.y},200);
	}

	keyDown(key) {
		var column = this._selectedIndex % this.maxColumn;
		var row = parseInt(this._selectedIndex / this.maxColumn);

		switch(key) {
			case "left": column = Math.max(0, column - 1); break;
			case "right": column = Math.min(this.maxColumn - 1, column + 1); break;
			case "up": row = Math.max(0, row - 1); break;
			case "down": row = Math.min(parseInt(global.party.length / this.maxColumn), row + 1); break;
			case "z": this.enity(this._selectedIndex); return;
			case "x": this.uimanager.removeChild(this.uimanager.children[this.uimanager.children.length - 1]); return;
		}

		var targetIndex = column + row * this.maxColumn;
		if(targetIndex < 0) targetIndex = 0;
		if(targetIndex >= global.party.length) targetIndex = global.party.length - 1;
		this.select(targetIndex);
	}

	update() {
		for(var i = 0; i < global.party.length; i++)
			global.party[i].update();
	}

	test() {
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/test.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/test.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/test.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/test.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/test.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
		global.party.push(L2D.Loader.load("res/npc.act"));
	}

	enity(index) {
		new Inventory(this.uimanager, index, this.getLocation(index));
	}

	reset() {
		const info = this.children[this.children.length - 1];
		this.removeChild(info);

		for(var i = 0; i < global.party.length; i++) {
			const character = global.party[i];
			character.state = "idea";
			character.centerPoint = this.getLocation(i);
			this.addChild(character);
		}

		this.addChild(info);
		this.info.position.x = -400;
		this.infoTween = createjs.Tween.get(this.info.position).to({x:0,y:this.info.position.y},200);
	}
}